package com.yibai.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yibai.web.app.entity.Banner;
import com.yibai.web.app.entity.Category;
import com.yibai.web.app.mapper.BannerMapper;
import com.yibai.web.app.service.BannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yibai.web.dto.BannerInfoRequest;
import com.yibai.web.dto.BannerListRequest;
import com.yibai.web.dto.BannerRequest;
import com.yibai.web.dto.SuperResponse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-04-19
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Override
    public SuperResponse list(BannerListRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<Banner> wrapper = new QueryWrapper();
        Page<Banner> pages = new Page<>(request.getPageNo(),request.getPageSize());
        IPage<Banner> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<Banner> agents = mapIPage.getRecords();
        response.setItems(agents);
        response.setTotals(pages.getTotal());
        return response;
    }

    @Override
    public SuperResponse info(BannerRequest request) {
        SuperResponse response = new SuperResponse();
        response.setItem(request.getId());
        return response;
    }

    @Override
    public SuperResponse add(BannerInfoRequest request) {
        SuperResponse response = new SuperResponse();
        Banner banner = new Banner();
        banner.setImageUrl(request.getImageUrl());
        banner.setJumpUrl(request.getJumpUrl());
        banner.setName(request.getName());
        banner.setSiteId(request.getSiteId());
        banner.setPosition(request.getPosition());
        banner.setRemarks(request.getRemarks());
        banner.setStatus(request.getStatus());
        baseMapper.insert(banner);
        return response;
    }

    @Override
    public SuperResponse update(BannerInfoRequest request) {
        SuperResponse response = new SuperResponse();
        Banner banner = new Banner();
        banner.setId(request.getId());
        banner.setImageUrl(request.getImageUrl());
        banner.setJumpUrl(request.getJumpUrl());
        banner.setName(request.getName());
        banner.setSiteId(request.getSiteId());
        banner.setPosition(request.getPosition());
        banner.setRemarks(request.getRemarks());
        banner.setStatus(request.getStatus());
        baseMapper.updateById(banner);
        return response;
    }

    @Override
    public SuperResponse delete(BannerRequest request) {
        SuperResponse response = new SuperResponse();
        baseMapper.deleteById(request.getId());
        return response;
    }
}
